﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Forms;
using System.Drawing;
using Silvermoon.Animations;

namespace Silvermoon.Controls.Textboxes
{
    public class TextSelectToolTip : ToolTip
    {
        const int ButtonWidth = 128;

        private static TextSelectToolTip instance;

        private BubbleButton paste, select, selectAll;

        public static TextSelectToolTip Instance
        {
            get
            {
                if (instance == null) instance = new TextSelectToolTip();
                return instance;
            }
        }

        private TextSelectToolTip()
            : base()
        {
            paste = new BubbleButton("Paste");
            select = new BubbleButton("Select");
            selectAll = new BubbleButton("Select All");


            paste.Tap += OnPaste;
            select.Tap += OnSelect;
            selectAll.Tap += OnSelectAll;

            Width = ButtonWidth;
        }

        public TextboxBase TextBox { get { return AnchorControl as TextboxBase; } }

        private void OnPaste(object sender, EventArgs e)
        {
            if (TextBox != null) TextBox.Paste();
            Hide();
        }

        private void OnSelect(object sender, EventArgs e)
        {
            if (TextBox != null) TextBox.SelectWord();
            Hide();
            TextBox.ShowClipboard();
        }

        private void OnSelectAll(object sender, EventArgs e)
        {
            if (TextBox != null) TextBox.SelectAll();
            Hide();
            TextBox.ShowClipboard();
        }

        protected override void OnHidden()
        {
            AnchorControl = null;
            base.OnHidden();
        }

        public void ShowTip()
        {
            Show();
        }

        protected override void OnAnchorControlChanging(Control newControl)
        {
            base.OnAnchorControlChanging(newControl);
            TextboxBase textBox = newControl as TextboxBase;
            Items.Clear();
            if (textBox != null)
            {
                bool readOnly = textBox.ReadOnly;
                if (!readOnly) Items.Add(paste);
                if (!string.IsNullOrEmpty(textBox.Text))
                {
                    Items.Add(select);
                    Items.Add(selectAll);
                }

            }
            Width = Items.Count * ButtonWidth;
        }
    }
}
